/*
 * @Author       : Joel
 * @Date         : 2025-03-31 09:35:23
 * @LastEditors  : Joel
 * @LastEditTime : 2025-03-31 13:54:06
 * @FilePath     : /usr/board_driver/button.c
 * @Description  : 
 * Copyright(C)2025 YiJiaLink, All Rights Reserved. 
 * 2025-03-31 09:35:23
 */


#include "button.h"
static uint8_t button_switch_index = 0;

static void APP_ConfigureEXTI(void)
{
  /* Enable GPIOA Clock */
  LL_IOP_GRP1_EnableClock(LL_IOP_GRP1_PERIPH_GPIOA);

  /* PA12 as input */
  LL_GPIO_InitTypeDef GPIO_InitStruct;
  GPIO_InitStruct.Pin = LL_GPIO_PIN_7;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  /* Triggerred by falling edge */
  LL_EXTI_InitTypeDef EXTI_InitStruct;
  EXTI_InitStruct.Line = LL_EXTI_LINE_7;
  EXTI_InitStruct.LineCommand = ENABLE;
  EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
  EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_RISING_FALLING;
  LL_EXTI_Init(&EXTI_InitStruct);

  /**
   * Enable interrupt
   * - EXTI0_1_IRQn for PA/PB/PC[0,1]
   * - EXTI2_3_IRQn for PA/PB/PC[2,3]
   * - EXTI4_15_IRQn for PA/PB/PC[4,15]
  */
  NVIC_SetPriority(EXTI4_15_IRQn, 0);
  NVIC_EnableIRQ(EXTI4_15_IRQn);
}
/**
 * @description: 初始化按键输入
 * @return {*}
 */
void button_init(void)
{
    APP_ConfigureEXTI();
    return;
}
uint8_t get_button(void)
{
    return  button_switch_index;
}
uint8_t set_button(uint8_t flag)
{
    return  button_switch_index = flag;
}
/**
 * @description: 中断进入
 * @return {*}
 */
void EXTI4_15_IRQHandler(void)
{
  if(LL_EXTI_IsActiveFlag(LL_EXTI_LINE_7))
  {
    button_switch_index=!button_switch_index;
    LL_EXTI_ClearFlag(LL_EXTI_LINE_7);
  }
}
